using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using System.Xml;
using System.ComponentModel;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:CompatibilityPanel runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class CompatibilityPanel : ARFPanel
    {
        private bool _aaCompliant = false;
        private bool _xhtmlCompliant = false;
        private bool _removeHtmlFieldDivs = false;
        private bool _removeWordMarkup = false;

        public bool RemoveWordMarkup
        {
            get { return _removeWordMarkup; }
            set { _removeWordMarkup = value; }
        }

        public bool RemoveHtmlFieldDivs
        {
            get { return _removeHtmlFieldDivs; }
            set { _removeHtmlFieldDivs = value; }
        }

        public bool XhtmlCompliant
        {
            get { return _xhtmlCompliant; }
            set { _xhtmlCompliant = value; }
        }

        public bool AACompliant
        {
            get { return _aaCompliant; }
            set { _aaCompliant = value; }
        }

        protected override bool CustomRendering
        {
            get
            {
                if (SPContext.Current.FormContext.FormMode != Microsoft.SharePoint.WebControls.SPControlMode.Display) return false;

                return (RemoveWordMarkup || XhtmlCompliant || AACompliant || RemoveHtmlFieldDivs);
            }
        }

        protected override string ModifyRenderedHtml(string html)
        {
            // TODO: Perform the fixes here

            //StringBuilder sb = new StringBuilder(base.ModifyRenderedHtml(html));

            if (RemoveWordMarkup)
            {
                string sRegEx = @"(?s)( class=\w+(?=([^&lt;]*&gt;)))|(&lt;!--\[if.*?&lt;!\[endif\]--&gt;)|(&lt;!\[if !\w+\]&gt;)|(&lt;!\[endif\]&gt;)|(&lt;o:p&gt;[^&lt;]*&lt;/o:p&gt;)|(&lt;span[^&gt;]*&gt;)|(&lt;/span&gt;)|(font-family:[^&gt;]*[;'])|(font-size:[^&gt;]*[;'])(?-s)";
                Regex r = new Regex(sRegEx);
                html = r.Replace(html, "");
            }

            if (XhtmlCompliant || AACompliant || RemoveHtmlFieldDivs)
            {
                StringBuilder oOutput = new StringBuilder();
                ARF.Web.Utility.HtmlReader oReader = new ARF.Web.Utility.HtmlReader(html);
                ARF.Web.Utility.HtmlWriter oWriter = new ARF.Web.Utility.HtmlWriter(oOutput);

                oReader.Read();
				while( !oReader.EOF ) oWriter.WriteNode(oReader, true);

                oWriter.Flush();
                oWriter.Close();

                html = oOutput.ToString();

                if (RemoveHtmlFieldDivs || AACompliant)
                {
                    XmlDocument oDoc = new XmlDocument();
                    oDoc.LoadXml("<XML>" + html + "</XML>");

                    if (RemoveHtmlFieldDivs) RemoveDivsFromHtmlField(oDoc);
                    if (AACompliant) FixHtmlForAA(oDoc);

                    html = oDoc.DocumentElement.InnerXml;
                }
            }

            return html;
        }

        protected virtual void FixHtmlForAA(XmlDocument oDoc)
        {
            
        }

        protected virtual void RemoveDivsFromHtmlField(XmlDocument oDoc)
        {
            XmlNodeList oList = oDoc.SelectNodes("//div[contains(@id, '_RichHtmlField')]");
            foreach (XmlElement oDiv in oList)
            {
                XmlDocumentFragment oFrag = oDoc.CreateDocumentFragment();
                oFrag.InnerXml = oDiv.InnerXml;

                oDiv.ParentNode.ReplaceChild(oFrag, oDiv);
            }
        }

    }
}
